package com.finger.gulimall.product.service.impl;

import com.finger.gulimall.product.entity.BrandEntity;
import com.finger.gulimall.product.entity.BrandVO;
import com.finger.gulimall.product.entity.CategoryEntity;
import com.finger.gulimall.product.exception.ProductException;
import com.finger.gulimall.product.service.BrandService;
import com.finger.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.Query;

import com.finger.gulimall.product.dao.CategoryBrandRelationDao;
import com.finger.gulimall.product.entity.CategoryBrandRelationEntity;
import com.finger.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    // 品牌服务
    @Autowired
    private BrandService brandService;

    // 分类服务
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 分页查询品牌分类中间表
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );
        // 返回
        return new PageUtils(page);
    }

    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        // 品牌id
        Long brandId = categoryBrandRelation.getBrandId();
        // 分类id
        Long catId = categoryBrandRelation.getCatelogId();
        // 根据品牌id和分类id在品牌分类中间表中查询该品牌是否存在
        Integer isExists = baseMapper.selectCount(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId).eq("catelog_id", catId));
        // 如果存在 则抛出异常
        if (isExists > 0) {
//            throw new ProductException("该分类下已存在该品牌");
        }

        // 根据品牌查找品牌名
        BrandEntity brand = brandService.getOne(
                new QueryWrapper<BrandEntity>().eq("brand_id", brandId).select("name")
        );
        // 根据分类id查找分类名
        CategoryEntity category = categoryService.getOne(
                new QueryWrapper<CategoryEntity>().eq("cat_id", catId).select("name")
        );
        // 设置品牌名
        categoryBrandRelation.setBrandName(brand.getName());
        // 设置分类名
        categoryBrandRelation.setCatelogName(category.getName());
        // 插入品牌和分类中间表数据
        baseMapper.insert(categoryBrandRelation);

    }

    @Override
    public void updateBrandDetails(Long brandId, String name) {
        // 创建品牌分类中间表实体
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        // 设置品牌名
        relationEntity.setBrandName(name);
        // 根据品牌id更新品牌分类中间表信息
        baseMapper.update(
                relationEntity,
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
        );
    }

    @Override
    public void updateCategoryDetails(Integer catId, String name) {
        // 创建品牌分类中间表实体
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        // 设置分类名
        relationEntity.setCatelogName(name);
        // 根据分类id更新 分类中间表实体数据
        baseMapper.update(
                relationEntity,
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId)
        );

    }

    @Override
    public List<BrandVO> getBrandsByCatId(Long catId) {
        // 创建查询条件
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        // 设置条件 根据分类id查询品牌id 和品牌名
        queryWrapper.eq("catelog_id", catId).select("brand_id", "brand_name");
        // 查询出该分类下的全部品牌
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(queryWrapper);
        // 使用stream把品牌分类中间表实体映射成传给前端的vo集合
        List<BrandVO> collect = categoryBrandRelationEntities.stream().map(item -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(item.getBrandId());
            brandVO.setBrandName(item.getBrandName());
            return brandVO;
        }).collect(Collectors.toList());
        // 返回
        return collect;
    }

}